package com.irony.eduservice.service.impl;

import com.irony.eduservice.entity.EduCourse;
import com.irony.eduservice.entity.EduCourseDescription;
import com.irony.eduservice.entity.vo.CourseInfoVo;
import com.irony.eduservice.mapper.EduCourseMapper;
import com.irony.eduservice.service.EduCourseDescriptionService;
import com.irony.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.irony.eduservice.service.EduSubjectService;
import com.irony.exception.OrangeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zgm
 * @since 2020-04-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.像课程添加基本信息
        //将vo转换成pojo
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        String subjectId = eduCourse.getSubjectId();
        int insert = baseMapper.insert(eduCourse);

        if (insert <= 0){
            throw new OrangeException(20001,"添加课程失败");
        }

        String id = eduCourse.getId();


        //2.向课程简介表添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(id);
        courseDescriptionService.save(courseDescription);

        return id;
    }
}
